package com.tlibrary.service;

import com.tlibrary.dao.AnsquestionsMapper;
import com.tlibrary.dao.LabelsMapper;
import com.tlibrary.dao.MultiplechoiceMapper;
import com.tlibrary.dao.SubquestionsMapper;
import com.tlibrary.model.MultiplechoiceWithBLOBs;
import com.tlibrary.util.GetQuestion;
import com.tlibrary.util.IsExitImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author：wcc
 * @Description：
 * @Date：Create in: 2019/1/3--9:35
 */
@Component
@Service
public class InsertService {

    @Autowired
    private MultiplechoiceMapper mm;
    @Autowired
    private GetQuestion getQuestion;
    @Autowired
    private IsExitImg isExitImg;
    @Autowired
    private LabelsMapper lm;
    @Autowired
    private SubquestionsMapper sm;

    @Autowired
    private AnsquestionsMapper am;

    @Transactional
    public int insertMultiplechoice(List<Map<String,String>> allMultipleChoice,HttpServletRequest request) {
        int num=0;
        MultiplechoiceWithBLOBs mc = new MultiplechoiceWithBLOBs();
        for (int i = 0; i < allMultipleChoice.size() ; i++) {
            Map<String, String> map = allMultipleChoice.get(i);
              mc.setTitle (map.get("title"));
              int j=1;
              while (j<=6){
                  switch (j){
                      case 1 :
                          mc.setA(map.get("con"+j));
                          break;
                      case 2 :
                          mc.setB(map.get("con"+j));
                          break;
                      case 3 :
                          mc.setC(map.get("con"+j));
                          break;
                      case 4 :
                          mc.setD(map.get("con"+j));
                          break;
                      case 5 :
                          mc.setE(map.get("con"+j));
                          break;
                      case 6 :
                          mc.setF(map.get("con"+j));
                          break;
                  }
                  j++;
              }
              mc.setId(UUID.randomUUID().toString());
               // mc.setUser(request.getSession().getAttribute("id").toString());
              mc.setUser("f9876a64-255c-4f05-837a-8a837477ec33");
              mc.setTime(new Date());
              try {
                  num += mm.insert(mc);
                  String nd = map.get("nd");
                  HashMap hashMap=new HashMap();
                  hashMap.put("nd",nd);
                  hashMap.put("ID",mc.getId());
                  lm.insertlabel(hashMap);

              }catch (Exception e){
                  e.printStackTrace();
              }
        }
        return num;
    }


    @Transactional
    public int insertDOC(String getall, Map<String, byte[]> pics, HttpServletResponse response, HttpServletRequest request) {
        int num=0;
        //选题题的录入
        List<Map<String,String>> allMultipleChoice=getQuestion.getAllMultipleChoice(getall);
        try {
            for (int i = 0; i <allMultipleChoice.size() ; i++) {
                MultiplechoiceWithBLOBs mc = new MultiplechoiceWithBLOBs();
                Map<String, String> map = allMultipleChoice.get(i);
                mc.setTitle (map.get("title"));
                String title = map.get("title");
                if(isExitImg.isExit(title)){
                    mc.setPictile(pics.get(isExitImg.getTab(title)));
                }
                int j=1;
                while (j<=6){
                    switch (j){
                        case 1 :
                            mc.setA(map.get("con"+j));
                            String s1 = map.get("con" + j);
                            if (s1==null){
                                break;
                            }
                            if(isExitImg.isExit(s1)){
                                mc.setPica(pics.get(isExitImg.getTab(s1)));
                            }
                            break;
                        case 2 :
                            mc.setB(map.get("con"+j));
                            String s2 = map.get("con" + j);
                            if (s2==null){
                                break;
                            }
                            if(isExitImg.isExit(s2)){
                                mc.setPicb(pics.get(isExitImg.getTab(s2)));
                            }
                            break;
                        case 3 :
                            mc.setC(map.get("con"+j));

                            String s3 = map.get("con" + j);
                            if (s3==null){
                                break;
                            }
                            if(isExitImg.isExit(s3)){
                                mc.setPicc(pics.get(isExitImg.getTab(s3)));
                            }
                            break;
                        case 4 :
                            mc.setD(map.get("con"+j));
                            String s4 = map.get("con" + j);
                            if (s4==null){
                                break;
                            }
                            if(isExitImg.isExit(map.get("con"+j))){
                                mc.setPicd(pics.get(isExitImg.getTab(s4)));
                            }
                            break;
                        case 5 :
                            mc.setE(map.get("con"+j));
                            String s5 = map.get("con" + j);
                            if (s5==null){
                                break;
                            }
                            if(isExitImg.isExit(s5)){
                                mc.setPice(pics.get(isExitImg.getTab(s5)));
                            }
                            break;
                        case 6 :
                            mc.setF(map.get("con"+j));
                            String s6 = map.get("con" + j);
                            if (s6==null){
                                break;
                            }
                            if(isExitImg.isExit(s6)){
                                mc.setPicf(pics.get(isExitImg.getTab(s6)));
                            }
                            break;
                    }
                    j++;
                }
                mc.setId(UUID.randomUUID().toString());
                mc.setTime(new Date());
                mc.setUser("f9876a64-255c-4f05-837a-8a837477ec33");
               // mc.setUser(request.getSession().getAttribute("id").toString());
                num += mm.insert(mc);
                String nd = map.get("nd");
                HashMap hashMap=new HashMap();
                hashMap.put("nd",nd);
                hashMap.put("ID",mc.getId());
                lm.insertlabel(hashMap);
            }
            //填空题的录入
            List<Map> fillins = getQuestion.getFillins(getall);

            for (Iterator<Map> iterator = fillins.iterator(); iterator.hasNext(); ) {
                HashMap next = (HashMap) iterator.next();
               // (#{id,jdbcType=VARCHAR},#{type,jdbcType=VARCHAR},#{nd,jdbcType=VARCHAR},#{content,jdbcType=VARCHAR},#{userid,jdbcType=VARCHAR},#{time,jdbcType=DATE})
               String userid="f9876a64-255c-4f05-837a-8a837477ec33";
               //userid=request.getSession().getAttribute("id").toString();f9876a64-255c-4f05-837a-8a837477ec33
                next.put("id",UUID.randomUUID().toString());
                next.put("time",new Date());
                next.put("type","填空题");
                next.put("userid",userid);
                sm.insertFillin(next);
                num++;
            }
            //判断题的录入
            List<Map> tfng = getQuestion.getTFNG(getall);
            for (Iterator<Map> iterator = tfng.iterator(); iterator.hasNext(); ) {
                HashMap next = (HashMap) iterator.next();
                // (#{id,jdbcType=VARCHAR},#{type,jdbcType=VARCHAR},#{nd,jdbcType=VARCHAR},#{content,jdbcType=VARCHAR},#{userid,jdbcType=VARCHAR},#{time,jdbcType=DATE})
                String userid="f9876a64-255c-4f05-837a-8a837477ec33";
                //userid=request.getSession().getAttribute("id").toString();f9876a64-255c-4f05-837a-8a837477ec33
                next.put("id",UUID.randomUUID().toString());
                next.put("time",new Date());
                next.put("userid",userid);
                next.put("type","判断题");
                sm.insertFillin(next);
                num++;
            }
            //简答题的录入

            List<Map> getquestion = getQuestion.getquestion(getall);
            for (Iterator<Map> iterator = getquestion.iterator(); iterator.hasNext(); ) {
                HashMap next = (HashMap) iterator.next();
                // (#{id,jdbcType=VARCHAR},#{type,jdbcType=VARCHAR},#{nd,jdbcType=VARCHAR},#{content,jdbcType=VARCHAR},#{userid,jdbcType=VARCHAR},#{time,jdbcType=DATE})
                String userid="f9876a64-255c-4f05-837a-8a837477ec33";
                //userid=request.getSession().getAttribute("id").toString();f9876a64-255c-4f05-837a-8a837477ec33
                next.put("id",UUID.randomUUID().toString());
                next.put("time",new Date());
                next.put("type","简答题");
                next.put("userid",userid);
                am.insertsub(next);
                num++;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    public int isertFillins(List<HashMap<String, String>> fillins, HttpServletRequest request) {
        int i=0;
        for (HashMap<String,String> map: fillins
             ) {
            HashMap hashMap=new HashMap();
            //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
            String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
            hashMap.put("userid",userid);
            hashMap.put("id",UUID.randomUUID().toString());
            hashMap.put("type","填空题");
            hashMap.put("content",map.get("tf"));
            hashMap.put("nd",map.get("nd"));
            hashMap.put("time",new Date());
            sm.insertFillin(hashMap);
            i++;
        }
        return i;
    }

    public int isertquestion(List<HashMap<String, String>> question, HttpServletRequest request)
    {
        int i=0;
        for (HashMap map: question ) {
            HashMap hashMap=new HashMap();
            //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
            String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
            hashMap.put("userid",userid);
            hashMap.put("id",UUID.randomUUID().toString());
            hashMap.put("type","简答题");
            hashMap.put("content",map.get("tf"));
            hashMap.put("nd",map.get("nd"));
            hashMap.put("time",new Date());
            am.insertsub(hashMap);
            i++;
        }
        return i;
    }

    public int iserttfng(List<HashMap<String,String>> tfng, HttpServletRequest request)
    {
        int i=0;
        for (HashMap<String,String> map: tfng
                ) {


            HashMap hashMap=new HashMap();
            //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
            String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
            hashMap.put("userid",userid);
            hashMap.put("id",UUID.randomUUID().toString());
            hashMap.put("type","判断题");
            hashMap.put("content",map.get("tf"));
            hashMap.put("nd",map.get("nd"));
            hashMap.put("time",new Date());

            sm.insertFillin(hashMap);
            i++;
        }
        return i;
    }
}
